/*
 * @LastEditTime: 2023-11-06 14:46:46
 * @FilePath: \bdcar-net\src\utils\clearSelect.js
 * @Description: 页面滚动时清除各种错位的el-elment气泡
 */
let mouseUp = null;
let mouseDown = null;

// 隐藏popper
export const hidePopper = function (e) {
    let dom = document.querySelector('.el-popper');
    if (!dom) {
        return;
    }
    // 创建鼠标事件
    if (!mouseUp || !mouseDown) {
        mouseUp = new MouseEvent('mouseup', {
            'view': window,
            'bubbles': true,
            'cancelable': true
        });
        mouseDown = new MouseEvent('mousedown', {
            'view': window,
            'bubbles': true,
            'cancelable': true
        });
    }
    // 顺序触发mouseDown、mouseUp
    dom.dispatchEvent(mouseDown);
    dom.dispatchEvent(mouseUp);
}

// 清除鼠标事件
export const delEvents = function () {
    console.log('-----delete events-----');
    mouseUp = null;
    mouseDown = null;
}

/**
  * 滚动时隐藏
  * @param contain 滚动监听的容器
  * @param cls 隐藏的元素class:如下拉、时间弹框等
*/
export const scrollHide = function (contain, cls) {
    // 先移除
    if (mouseUp || mouseDown) {
        delEvents();
    }
    // 再绑定
    contain.onscroll = function () {
        hidePopper(cls);
    }
}